package com.ruoyi.order.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.TechnicianItemOrder;
import com.ruoyi.order.mapper.TechnicianItemOrderMapper;
import com.ruoyi.order.service.ITechnicianItemOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 常规订单Controller
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/technicianItemOrder/technicianItemOrder")
public class TechnicianItemOrderController extends BaseController {
    @Autowired
    private ITechnicianItemOrderService technicianItemOrderService;
    @Resource
    private TechnicianItemOrderMapper technicianItemOrderMapper;

    /**
     * 查询常规订单列表
     */
    @PreAuthorize("@ss.hasPermi('technicianItemOrder:technicianItemOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(TechnicianItemOrder technicianItemOrder) {
        startPage();
        List<TechnicianItemOrder> list = technicianItemOrderService.selectTechnicianItemOrderList(technicianItemOrder);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianItemOrder itemOrder : list) {
                String tName = technicianItemOrderMapper.selectTName(itemOrder.getTid());
                itemOrder.setTName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                String tTel = technicianItemOrderMapper.selectTTel(itemOrder.getTid());
                itemOrder.setTTel(CheckEmptyUtil.isEmpty(tTel) ? "" : tTel);
                String tAvatar = technicianItemOrderMapper.selectTAvatar(itemOrder.getTid());
                itemOrder.setTAvatar(CheckEmptyUtil.isEmpty(tAvatar) ? "" : tAvatar);
                String userName = technicianItemOrderMapper.selectUserName(itemOrder.getUserId());
                itemOrder.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
                String userTel = technicianItemOrderMapper.selectUserTel(itemOrder.getUserId());
                itemOrder.setUserTel(CheckEmptyUtil.isEmpty(userTel) ? "" : userTel);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出常规订单列表
     */
    @PreAuthorize("@ss.hasPermi('technicianItemOrder:technicianItemOrder:export')")
    @Log(title = "常规订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TechnicianItemOrder technicianItemOrder) {
        List<TechnicianItemOrder> list = technicianItemOrderService.selectTechnicianItemOrderList(technicianItemOrder);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianItemOrder itemOrder : list) {
                String tName = technicianItemOrderMapper.selectTName(itemOrder.getTid());
                itemOrder.setTName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                String tTel = technicianItemOrderMapper.selectTTel(itemOrder.getTid());
                itemOrder.setTTel(CheckEmptyUtil.isEmpty(tTel) ? "" : tTel);
                String tAvatar = technicianItemOrderMapper.selectTAvatar(itemOrder.getTid());
                itemOrder.setTAvatar(CheckEmptyUtil.isEmpty(tAvatar) ? "" : tAvatar);
                String userName = technicianItemOrderMapper.selectUserName(itemOrder.getUserId());
                itemOrder.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
                String userTel = technicianItemOrderMapper.selectUserTel(itemOrder.getUserId());
                itemOrder.setUserTel(CheckEmptyUtil.isEmpty(userTel) ? "" : userTel);
            }
        }
        ExcelUtil<TechnicianItemOrder> util = new ExcelUtil<TechnicianItemOrder>(TechnicianItemOrder.class);
        util.exportExcel(response, list, "常规订单数据");
    }

    /**
     * 获取常规订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('technicianItemOrder:technicianItemOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TechnicianItemOrder technicianItemOrder = technicianItemOrderService.selectTechnicianItemOrderById(id);
        if (!CheckEmptyUtil.isEmpty(technicianItemOrder)) {

            String tName = technicianItemOrderMapper.selectTName(technicianItemOrder.getTid());
            technicianItemOrder.setTName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            String tTel = technicianItemOrderMapper.selectTTel(technicianItemOrder.getTid());
            technicianItemOrder.setTTel(CheckEmptyUtil.isEmpty(tTel) ? "" : tTel);
            String tAvatar = technicianItemOrderMapper.selectTAvatar(technicianItemOrder.getTid());
            technicianItemOrder.setTAvatar(CheckEmptyUtil.isEmpty(tAvatar) ? "" : tAvatar);
            String userName = technicianItemOrderMapper.selectUserName(technicianItemOrder.getUserId());
            technicianItemOrder.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
            String userTel = technicianItemOrderMapper.selectUserTel(technicianItemOrder.getUserId());
            technicianItemOrder.setUserTel(CheckEmptyUtil.isEmpty(userTel) ? "" : userTel);
            technicianItemOrder.setPlaceOrderCount(technicianItemOrderMapper.selectUserPlaceOrderCount(technicianItemOrder.getUserId()));
            String labeList = technicianItemOrderMapper.selectUserLabeList(technicianItemOrder.getUserId());
            if (!CheckEmptyUtil.isEmpty(labeList)) {
                technicianItemOrder.setUserLabelName(Arrays.stream(labeList.split(","))
                        .collect(Collectors.toList()));
            }
            BigDecimal itemIncome = technicianItemOrderMapper.selectTechnicianItemIncome(technicianItemOrder.getOrderNo());
            technicianItemOrder.setTechnicianItemIncome(CheckEmptyUtil.isEmpty(itemIncome) ? BigDecimal.ZERO : itemIncome);
            BigDecimal carIncome = technicianItemOrderMapper.selectTechnicianCarIncome(technicianItemOrder.getOrderNo());
            technicianItemOrder.setTechnicianCarIncome(CheckEmptyUtil.isEmpty(carIncome) ? BigDecimal.ZERO : carIncome);

            BigDecimal brokerIncome = technicianItemOrderMapper.selectBrokerIncome(technicianItemOrder.getOrderNo());
            technicianItemOrder.setBrokerIncome(CheckEmptyUtil.isEmpty(brokerIncome) ? BigDecimal.ZERO : brokerIncome);
            BigDecimal salesmanIncome = technicianItemOrderMapper.selectSalesmanIncome(technicianItemOrder.getOrderNo());
            technicianItemOrder.setSalesmanIncome(CheckEmptyUtil.isEmpty(salesmanIncome) ? BigDecimal.ZERO : salesmanIncome);
            BigDecimal channelIncome = technicianItemOrderMapper.selectChannelIncome(technicianItemOrder.getOrderNo());
            technicianItemOrder.setChannelIncome(CheckEmptyUtil.isEmpty(channelIncome) ? BigDecimal.ZERO : channelIncome);
            if (technicianItemOrder.getIsUserCoupon() == 1) {
                BigDecimal couponPrice = technicianItemOrderMapper.selectCouponPrice(technicianItemOrder.getUserCouponId());
                technicianItemOrder.setCouponPrice(CheckEmptyUtil.isEmpty(couponPrice) ? BigDecimal.ZERO : couponPrice);
            } else {
                technicianItemOrder.setCouponPrice(BigDecimal.ZERO);
            }

            String brokerName = technicianItemOrderMapper.selectBrokerName(technicianItemOrder.getTid());
            technicianItemOrder.setBrokerName(CheckEmptyUtil.isEmpty(brokerName) ? "" : brokerName);

            String channelName = technicianItemOrderMapper.selectChannelName(technicianItemOrder.getTid());
            technicianItemOrder.setChannelName(CheckEmptyUtil.isEmpty(channelName) ? "" : channelName);

            String salesmanName = technicianItemOrderMapper.selectSalesmanName(technicianItemOrder.getTid());
            technicianItemOrder.setSalesmanName(CheckEmptyUtil.isEmpty(salesmanName) ? "" : salesmanName);
            if (technicianItemOrder.getOrderStatus() == 0) {
                technicianItemOrder.setCountdown((DateUtil.between(new Date(), DateUtil.offsetMinute(technicianItemOrder.getPlaceOrderTime(), 10), DateUnit.MS, false)) <= 0 ? 0 : DateUtil.between(new Date(), DateUtil.offsetMinute(technicianItemOrder.getPlaceOrderTime(), 10), DateUnit.MS, false));
            }
            Integer serviceTime = technicianItemOrder.getItemTime();
            TechnicianItemOrder upgradeOrder = technicianItemOrderMapper.selectUpgradeOrder(technicianItemOrder.getOrderNo());
            if (!CheckEmptyUtil.isEmpty(upgradeOrder)) {
                serviceTime = upgradeOrder.getItemTime();
            }
            List<TechnicianItemOrder> bellOrder = technicianItemOrderMapper.selectBellOrder(technicianItemOrder.getOrderNo());
            if (!CheckEmptyUtil.isEmpty(bellOrder)) {
                serviceTime += bellOrder.stream().mapToInt(TechnicianItemOrder::getItemTime).sum();
            }
            if (!CheckEmptyUtil.isEmpty(technicianItemOrder.getStartServiceTime())) {

                technicianItemOrder.setServiceOverCountDown((DateUtil.between(new Date(), DateUtil.offsetMinute(technicianItemOrder.getStartServiceTime(), serviceTime), DateUnit.MS, false)) <= 0 ? 0 : DateUtil.between(new Date(), DateUtil.offsetMinute(technicianItemOrder.getStartServiceTime(), serviceTime), DateUnit.MS, false));
            }
            if (technicianItemOrder.getOrderStatus()==13 || technicianItemOrder.getOrderStatus()==11 || technicianItemOrder.getOrderStatus()==12){
                Integer refundOrderStatus = technicianItemOrderMapper.seleRefundOrderStatus(technicianItemOrder.getOrderNo());
                technicianItemOrder.setOrderOldStatus(refundOrderStatus);
            }

        }
        return success(technicianItemOrder);
    }

    /**
     * 新增常规订单
     */
    @PreAuthorize("@ss.hasPermi('technicianItemOrder:technicianItemOrder:add')")
    @Log(title = "常规订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TechnicianItemOrder technicianItemOrder) {
        return toAjax(technicianItemOrderService.insertTechnicianItemOrder(technicianItemOrder));
    }

    /**
     * 修改常规订单
     */
    @PreAuthorize("@ss.hasPermi('technicianItemOrder:technicianItemOrder:edit')")
    @Log(title = "常规订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TechnicianItemOrder technicianItemOrder) {
        return toAjax(technicianItemOrderService.updateTechnicianItemOrder(technicianItemOrder));
    }

    /**
     * 删除常规订单
     */
    @PreAuthorize("@ss.hasPermi('technicianItemOrder:technicianItemOrder:remove')")
    @Log(title = "常规订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(technicianItemOrderService.deleteTechnicianItemOrderByIds(ids));
    }
}
